import React, {useEffect} from "react";
import useGetUserInfo from "./useGetUserInfo";
import {useLocation, useNavigate} from "react-router-dom";
import {isLoginOrRegister, isNoNeedUserInfo, LOGIN_PATH, MANAGE_LIST_PATH} from "../router";


function useNavPage(waiting:boolean){
         const {username} = useGetUserInfo();
         const nav = useNavigate();
         const {pathname} = useLocation();
         useEffect(()=>{
             if(waiting) {
                 return;
             }
             //有username,说明登入了
             if(username){
                 if(isLoginOrRegister(pathname)){
                     nav(MANAGE_LIST_PATH);
                 }
                 return;
             }
             //没有登入
             if(isNoNeedUserInfo(pathname)) {
                 return;
             }else {
                 nav(LOGIN_PATH);
             }
         },[waiting,username,pathname])
}


export default useNavPage;